//
//  WSSureOrderFooterView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/5.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSSureOrderFooterView.h"
@interface WSSureOrderFooterView()
//alloc 8 次
/**div0 添加手势 */
@property (nonatomic, strong) UIView *div0;
/**优惠金额 默认影藏*/
@property (nonatomic, strong) UILabel *couponPriceLb;


//--------------------
/**1.请选择支付方式*/

@property (nonatomic, strong) UILabel *payTypeLb;
/**线上按钮*/
@property (nonatomic, strong) UIButton *onLineBtn;
/**线下按钮*/
@property (nonatomic, strong) UIButton *offLineBtn;

//--------------------
/**2.备注*/
@property (nonatomic, strong) UIView *div2;
/**2-1*/
@property (nonatomic, strong) UILabel *remarkLb;

// 可用优惠券 > 箭头1
@property (nonatomic, strong) UIImageView *arrowImg1;
//箭头2
@property (nonatomic, strong) UIImageView *arrowImg2;


//!!!!:11-29 支付方式dive
/**支付方式div*/
@property (nonatomic, strong) UIView *paytypeDiv;

@end

@implementation WSSureOrderFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        self.backgroundColor = kMainLightGrayColor; //灰色背景，用于实现分割线的效果
    }
    return self;
}

- (void)setupView  {
    [self addSubview:self.div0];
    [self addSubview:self.paytypeDiv]; //支付方式
    
    
    [self addSubview:self.div2];

    //div0
    [self.div0 addSubview:self.avaliableCouponLb];
    [self.div0 addSubview:self.arrowImg1];//可用优惠券右边箭头
    [self.div0 addSubview:self.couponPriceLb];
    //==========================
    
    //支付方式
    [self.paytypeDiv addSubview:self.payTypeLb];
    [self.paytypeDiv addSubview:self.onLineBtn];
    [self.paytypeDiv addSubview:self.offLineBtn];

    
    //==========================
    
    //div2
    [self.div2 addSubview:self.remarkLb];
    [self.div2 addSubview:self.markTF];
}

//!!!!:layout
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat sw = self.bounds.size.width;
    
    CGFloat h = 44;
    self.div0.frame = CGRectMake(0, 10, sw, h);
    CGFloat h2 = 22;
    CGFloat w2 = 150;
    CGFloat x = 15;
    CGFloat y = (h - h2)*0.5;
    
//    self.avaliableCouponLb.frame = CGRectMake(x, y, w2, h2);
//    self.avaliableCouponLb.backgroundColor = [UIColor lightGrayColor];
    [self.avaliableCouponLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div0.mas_left).offset(15);
        make.height.mas_equalTo(22);
        make.centerY.equalTo(self.div0.mas_centerY);
        make.width.mas_greaterThanOrEqualTo(100); //2019-2-23自适应宽度
    }];
//
    [self.couponPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_greaterThanOrEqualTo(20);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(self.div0.mas_centerY);
        make.right.equalTo(self.arrowImg1.mas_left);
    }];
    //可用优惠券箭头
    [self.arrowImg1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.div0.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.equalTo(self.div0.mas_right).offset(-15);
    }];
    

    
    //self的背景为灰色 ， 留出 1px 实现 分割线效果
//    self.div1.frame = CGRectMake(0, self.div0.rb_bottom + 1, sw, h);
    
    self.paytypeDiv.frame = CGRectMake(0, self.div0.rb_bottom+1, sw, h);
//    self.payTypeLb.frame = CGRectMake(x, y, 90, h2);
    [self.payTypeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.paytypeDiv.mas_left).offset(x);
        make.centerY.equalTo(self.paytypeDiv.mas_centerY);
        make.height.mas_equalTo(h2);
        make.width.mas_greaterThanOrEqualTo(90); //2019-2-22 国际化后显示不全
    }];
    
    [self.onLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.offLineBtn.mas_left);
        make.centerY.equalTo(self.paytypeDiv.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    [self.offLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.paytypeDiv.mas_right).offset(-10);
        make.size.mas_equalTo(CGSizeMake(80, 20));
        make.centerY.equalTo(self.paytypeDiv.mas_centerY);
    }];
//    self.pleaseSelectePayTypeLb.frame = CGRectMake(self.payTypeLb.rb_right, y, sw-self.payTypeLb.rb_right-10-15, h2);
//    self.arrowImg2.frame = CGRectMake(sw - 15-10, imgY, 15,15);
//    self.arrowImg2.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
    [self.onLineBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [self.offLineBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    
    
    
    //==============
    self.div2.frame = CGRectMake(0, self.paytypeDiv.rb_bottom + 1, sw, h);
    self.remarkLb.frame = CGRectMake(x, y, 90, h2);
    
    // 宽度
    CGFloat markTFW = sw-self.remarkLb.rb_right -10;
    self.markTF.frame = CGRectMake(self.remarkLb.rb_right, y, markTFW, h2);
}

#pragma mark - getter
- (UIView *)div0 {
    if (!_div0) {
        _div0 = [UIView new];
        _div0.backgroundColor = [UIColor whiteColor];
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAvaliableCouponGes:)];
        [_div0 addGestureRecognizer:tap];
    }
    return _div0;
}
- (UILabel *)avaliableCouponLb {
    if (!_avaliableCouponLb) {
        _avaliableCouponLb = [UILabel new];
        _avaliableCouponLb.textColor = [UIColor redColor];
        
        _avaliableCouponLb.text = NSLocalizedString(Order_AvaliableCoupon, @"可用优惠券");
    }
    return _avaliableCouponLb;
}
//优惠金额
- (UILabel *)couponPriceLb {
    if (!_couponPriceLb) {
        _couponPriceLb = [[UILabel alloc] init];
//        _couponPriceLb.backgroundColor = [UIColor lightGrayColor];
        _couponPriceLb.text = @"€1.2";
        _couponPriceLb.textColor = kMainRedColor;
        _couponPriceLb.hidden = YES; //2019-1-15默认隐藏。
    }
    return _couponPriceLb;
}


- (UILabel *)payTypeLb {
    if (!_payTypeLb) {
        _payTypeLb = [UILabel new];
        _payTypeLb.text = NSLocalizedString(Order_PayType, @"支付方式");
    }
    return _payTypeLb;
}

- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
        _div2.backgroundColor = [UIColor whiteColor];
    }
    return _div2;
}

- (UILabel *)remarkLb {
    if (!_remarkLb) {
        _remarkLb = [UILabel new];
        _remarkLb.text = NSLocalizedString(Order_Remark, @"备注");
    }
    return _remarkLb;
}
- (UITextField *)markTF {
    if (!_markTF) {
        _markTF = [[UITextField alloc] init];
//        _markTF.placeholder = NSLocalizedString(Order_RemarkTips, @"口味、偏好等要求");
        //2019-2-25 预约订单不要提提示语
        _markTF.placeholder = NSLocalizedString(L_KeTianXieYueDinSongCanShiJian, @"可填写预定送餐时间等");
        _markTF.textAlignment = NSTextAlignmentRight;
    }
    return _markTF;
}

- (UIImageView *)arrowImg1 {
    if (!_arrowImg1) {
        UIImage *img = [UIImage imageNamed:@"可用优惠券箭头.png"];  //>
        _arrowImg1 = [[UIImageView alloc] initWithImage:img];
    }
    return _arrowImg1;
}
- (UIImageView *)arrowImg2 {
    if (!_arrowImg2) {
        UIImage *img = [UIImage imageNamed:@"kai"];  // V
        _arrowImg2 = [[UIImageView alloc] initWithImage:img];
    }
    return _arrowImg2;
}

//!!!!:11-29支付方式变了(支付方式、)
- (UIView *)paytypeDiv {
    if (!_paytypeDiv) {
        _paytypeDiv = [UIView new];
        _paytypeDiv.backgroundColor = [UIColor whiteColor];
    }
    return _paytypeDiv;
}
- (UIButton *)onLineBtn {
    if (!_onLineBtn) {
        _onLineBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_onLineBtn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_onLineBtn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
        [_onLineBtn setTitle:NSLocalizedString(L_XianShang, @"线上") forState:(UIControlStateNormal)];
        [_onLineBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_onLineBtn addTarget:self action:@selector(clickOnLineAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _onLineBtn;
}

- (UIButton *)offLineBtn {
    if (!_offLineBtn) {
        _offLineBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_offLineBtn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_offLineBtn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
        [_offLineBtn setTitle:NSLocalizedString(L_XianXia, @"线下") forState:(UIControlStateNormal)];
        [_offLineBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_offLineBtn addTarget:self action:@selector(clickOffLineAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _offLineBtn;
}


#pragma mark - 点击事件
- (void)clickAvaliableCouponGes:(UITapGestureRecognizer *)tap {
    FDLog(@"点击可用优惠券");
    if (self.clickAvaliableCouponBlock) {
        self.clickAvaliableCouponBlock();
    }
}

//线上
- (void)clickOnLineAction:(UIButton *)btn {
    FDLog(@"线上");
    btn.selected = YES;
    self.offLineBtn.selected = NO;
    
    if (self.clickOnLineBlock) {
        self.clickOnLineBlock();
    }
}

//线下
- (void)clickOffLineAction:(UIButton *)btn {
    FDLog(@"线下");
    
    btn.selected = YES;
    self.onLineBtn.selected = NO;
    
    if (self.clickOffLineBlock) {
        self.clickOffLineBlock();
    }
}



#pragma mark - 2019-1-15 更新页面
/**可用优惠券数量*/
- (void)updateAvaliableCouponCount:(NSInteger)count {
    self.avaliableCouponLb.text = [NSString stringWithFormat:@"%@(%ld)",NSLocalizedString(Order_AvaliableCoupon, @"可用优惠券"),count];
}

///**优惠金额*/
- (void)updateCouponPrice:(CGFloat)coupon {
    self.couponPriceLb.hidden = NO; //显示
    self.couponPriceLb.text =  [NSString stringWithFormat:@"-€%.1f",coupon];
}


@end



